package com.system.honour.controller;

import com.system.honour.common.ResultDTO;
import com.system.honour.entity.SysPermission;
import com.system.honour.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/sys/permission")
@Api(tags = "权限管理")
@Slf4j
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "查询树形结构全部权限")
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public ResultDTO getAll(){
        ResultDTO resultDTO = permissionService.findByAll();
        return resultDTO;
    }

    @ApiOperation(value = "根据角色id查询权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "角色id",paramType = "query",dataType ="int")
    })
    @RequestMapping(value = "/getPermissions",method = RequestMethod.GET)
    public ResultDTO getPermissionsByRoleId(Integer roleId){
        ResultDTO resultDTO = permissionService.getListByRoleId(roleId);
        return resultDTO;
    }

    @ApiOperation(value = "查询全部权限")
    @RequestMapping(value = "/MenuAll",method = RequestMethod.GET)
    public ResultDTO getMenuAll(){
        ResultDTO resultDTO = permissionService.findMenuAll();
        return resultDTO;
    }

    @ApiOperation(value = "添加权限")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ResultDTO savePermission(@RequestBody SysPermission permission){
        ResultDTO resultDTO = permissionService.save(permission);
        return resultDTO;
    }

    @ApiOperation(value = "编辑权限")
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public ResultDTO updatePermission(@RequestBody SysPermission permission){
        ResultDTO resultDTO = permissionService.update(permission);
        return resultDTO;
    }

    @ApiOperation(value = "删除权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "权限id",required = true,paramType = "query",dataType ="int")
    })
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public ResultDTO deletePermission(Integer id){
        ResultDTO resultDTO = permissionService.deleteById(id);
        return resultDTO;
    }

    @ApiOperation(value = "根据召唤师id获取对应的菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "召唤师id",required = true,paramType = "query",dataType ="int")
    })
    @RequestMapping(value = "/menu",method = RequestMethod.GET)
    public ResultDTO getMenu(Integer userId){
        ResultDTO resultDTO = permissionService.getMenuByUserId(userId);
        return resultDTO;
    }
}
